/*
 * marketsim-eclipsetrader
 *
 * Copyright (C) 2008-2011 Steve Phelps and John Cartlidge
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 */

package victria.marketsim.eclipsetrader.feed;

import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

import org.apache.log4j.Logger;

import victria.marketsim.core.Order;
import net.sourceforge.eclipsetrader.core.ILevel2Feed;
import net.sourceforge.eclipsetrader.core.db.Level2;
import net.sourceforge.eclipsetrader.core.db.Level2Bid;
import net.sourceforge.eclipsetrader.core.db.Level2Ask;
import net.sourceforge.eclipsetrader.core.db.Security;
import net.sourceforge.eclipsetrader.core.db.feed.Quote;


public class SimLevel2Feed extends AbstractFeed implements ILevel2Feed, Runnable {
	
	static Logger logger = Logger.getLogger(SimLevel2Feed.class);
	
	public void convert(Collection<Order> shouts,  Level2 level2) {
		for(Order shout : shouts) {
			level2.add(shout.getPrice(), shout.getQuantity());
		}
	}
	
	public void refresh() {
		for (Security security : securities) {
			Level2Bid level2Bids = new Level2Bid();
			Level2Ask level2Asks = new Level2Ask();
			convert(marketService.getAsks(), level2Asks);
			convert(marketService.getBids(), level2Bids);			
			security.setLevel2(level2Bids, level2Asks);	
		}
	}
	
	public void subscribeLevel2(Security security) {
		logger.debug("Subscribing to " + security);
		subscribe(security);
	}

	public void unSubscribeLevel2(Security security) {
		logger.debug("Unsubscribing from " + security); 
		unSubscribe(security);
	}
	
	public void snapshotLevel2() {
		refresh();
	}

	@Override
	public void startLevel2() {
		start();
	}	
	
	public void stopLevel2() {
		stop();
	}
	
}
